<template>
    <div class="login-outer">
    <i-form v-ref:form-inline :model="formInline" :rules="ruleInline">
        <Form-item prop="user">
            <i-input type="text" :value.sync="formInline.user" placeholder="Username">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </i-input>
        </Form-item>
        <Form-item prop="password">
            <i-input type="password" :value.sync="formInline.password" placeholder="Password">
                <Icon type="ios-locked-outline" slot="prepend"></Icon>
            </i-input>
        </Form-item>
        <Form-item>
            <Row>
                <i-col span="18">
                    <i-input :value.sync="formInline.security" placeholder="请输入验证码"></i-input>
                </i-col>
                <i-col span="6">
                    <i-button type="info" @click="reqCode">验证码</i-button>
                </i-col>
            </Row>
            
            
        </Form-item>
        <Form-item>
            <i-button type="primary" @click="handleSubmit('formInline')">登录</i-button>
        </Form-item>
    </i-form>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                formInline: {
                    user: 'COM_TEST',
                    password: '123456',
                    security: '',
                },
                ruleInline: {
                    user: [
                        { required: true, message: '请填写用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请填写密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit(name) {
                const that=this;
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$http.jsonp(this.path+'loginServlet/checkLogin',{
                                params:{userName:this.formInline.user,password:this.formInline.password,sendCode:this.formInline.security},
                                jsonp:'callback'
                            }).then((datas)=>{
                                console.log(datas);
                                if(datas.body.code=='001'){    
                                        this.store.state.user.company_code=datas.body.result.login_no;
                                        this.store.roll=datas.body.result.is_xiaoshou;
                                        this.store.state.logined=true;
                                        if(this.store.roll=="0"){
                                            this.myRouter.go({path:'/app/home'});
                                        }else if(this.store.roll=="1"||this.store.roll=="2"||this.store.roll=="3"){
                                            this.myRouter.go({path:'/appSale/productList'});
                                        }
                                }else{
                                    this.$Message.error('登陆失败!');
                                } 
                            },(error)=>{
                                this.$Message.error('提交成功!');
                            });    
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            reqCode(){
                this.$http.jsonp(this.path+'loginServlet/login',{
                            params:{userName:this.formInline.user,password:this.formInline.password},
                            jsonp:'callback'
                        }).then((datas)=>{
                            if(datas.body.code=='001'){
                                this.store.state.user.company_tel=datas.body.result.company_tel;
                                this.$http.jsonp(this.path+'loginServlet/sendCode',{
                                        params:{company_tel:this.store.state.user.company_tel},
                                        jsonp:'callback'
                                    }).then((datas)=>{
                                        console.log(datas);
                                        if(datas.body.code=='001'){
                                            this.$Message.success('验证码发送成功!');
                                        }else{
                                            this.$Message.error('验证码发送失败!');
                                        }
                                    },(error)=>{
                                        this.$Message.error('验证码请求失败!');
                                    });
                            }else{
                                this.$Message.error('用名或密码错误!');
                            }
                        },(error)=>{
                            this.$Message.error('网络问题!');
                        });
            },
            getBasic(){
                this.$http.jsonp(this.path+'company/companyDownServlet/toDetailPage',{
                    params:{company_code:this.store.state.user.company_code,},
                    jsonp:'callback'
                 }).then(
                    (datas)=>{
                        console.log(datas);
                        if(datas.body.code=='001'){
                            this.store.state.user.companyId=datas.body.result.id;
                            this.store.state.user.companyName=datas.body.result.company_name;
                            this.store.state.allInfor=datas.body.result;
                            this.myRouter.go({path:'/app/home'});
                        }else if(datas.body.code=='002'){
                            this.$Message.success('提交信息出错!');
                        }else{
                            this.$Message.success('奇怪错误!');
                        }
                    },
                    (error)=>{
                        this.$Message.success('网络问题!');
                    }
                 );
            }
        }
    }
</script>
<style>
    .login-outer{
        width:30%;
        position:relative;
        top:200px;
        margin:0 auto;
        text-align: center;
    }
</style>

